package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.*;
import cn.getech.data.development.mapper.*;
import cn.getech.data.development.model.vo.TableStatisticsVO;
import cn.getech.data.development.service.TableInfoService;
import cn.getech.data.development.service.TableStatisticsService;
import cn.getech.data.intelligence.common.utils.ByteConverFormat;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TableStatisticsServiceImpl extends ServiceImpl<TableStatisticsMapper, TableStatistics> implements TableStatisticsService {

    @Resource
    private TableStatisticsMapper mapper;
    @Autowired
    private TableInfoService tableInfoService;

    @Override
    public TableStatisticsVO saveOfYesterday(Integer tableId) {
        TableStatisticsVO vo = new TableStatisticsVO();
        try {
            TableInfo tableInfo = tableInfoService.getTableById(tableId);
            if (tableInfo != null) {
                if (tableInfo.getRowsNum() != null) {
                    vo.setRowNum(tableInfo.getRowsNum().intValue());
                }
                vo.setCreateTime(tableInfo.getCreateTime());
                vo.setTableName(tableInfo.getTableName());
                vo.setCreatePerName(tableInfo.getCreatePerName());
                vo.setTableId(tableInfo.getId());
            }
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("table_id", tableId);
            wrapper.orderByDesc("count_time");
//            wrapper.last("limit 2");
            List<TableStatistics> list = mapper.selectList(wrapper);
            if (CollectionUtil.isNotEmpty(list)) {
                Map<String, TableStatistics> map = new HashMap<>();
                DateTime dateTime;
                for (TableStatistics item : list) {
                    dateTime = new DateTime(item.getCountTime());
                    map.put(dateTime.toString("yyyyMMdd"), item);
                }
                DateTime today = DateTime.now();
                DateTime yesterday = DateUtil.yesterday();
                TableStatistics todayData = map.get(today.toString("yyyyMMdd"));
                TableStatistics yesterdayData = map.get(yesterday.toString("yyyyMMdd"));
                if (yesterdayData == null) {
                    yesterdayData = new TableStatistics();
                    yesterdayData.setRowsNum(0);
                    yesterdayData.setTotalSize(0.0);
                    yesterdayData.setPartitionCount(0);
                }
                if (todayData != null) {
                    DateTime countTime = new DateTime(todayData.getCountTime());
                    vo.setCountTime(countTime.toString("yyyy-MM-dd HH:mm:ss"));
                    vo.setTableId(todayData.getTableId());
                    if (todayData.getRowsNum() != null && todayData.getRowsNum() != 0) {
                        vo.setRowNum(todayData.getRowsNum().intValue());
                        int yesterdayRowNum = yesterdayData.getRowsNum() == null ? 0 : yesterdayData.getRowsNum().intValue();
                        int num = vo.getRowNum() - yesterdayRowNum;
                        vo.setAddRowNum(num);
                    }
                    if (todayData.getTotalSize() != null && todayData.getTotalSize() > 0) {
                        String todaySizeStr = todayData.getTotalSize() == null ? "0.0" : String.valueOf(todayData.getTotalSize());
                        BigDecimal todaySize = new BigDecimal(todaySizeStr);
                        String yesterdayStr = yesterdayData.getTotalSize() == null ? "0.0" : String.valueOf(yesterdayData.getTotalSize());
                        BigDecimal yesterdaySize = new BigDecimal(yesterdayStr);
                        BigDecimal result = todaySize.subtract(yesterdaySize);
                        double size = result.doubleValue();
                        vo.setAddSize(size);
                        vo.setAddSizeStr(ByteConverFormat.convertFormat(vo.getAddSize()));
                    }
                    vo.setPartitionCount(todayData.getPartitionCount());
                }
            }
        } catch (Exception e) {
            log.error("获取较昨日新增数据报错：{}", e);
        }
        return vo;
    }
}
